﻿using DataAccess.DTO;
using DataAccess.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XYD.DataAccess;
using XYD.Framework.Data;
using XYD.Framework.Data.Dapper;

namespace DataAccess.DAL
{
    public class RobotDAL : BaseDAL
    {
        public Robot Get(string activation)
        {
            return CONN_TopicSharing.SQLQuery<Robot>(string.Format(" select top 1 * from Robot where activation='{0}' ", activation), null).FirstOrDefault();
        }

        public Robot Get(string account ,string password)
        {
            return CONN_TopicSharing.SQLQuery<Robot>(string.Format(" select top 1 * from Robot where account='{0}' and password='{1}' and state=1 and is_delete=0 ", account, password), null).FirstOrDefault();
        }

        public bool Insert(Robot Robot)
        {
            return CONN_TopicSharing.Insert<Robot>(Robot);
        }

        public bool Update(Robot Robot)
        {
            return CONN_TopicSharing.Update<Robot>(Robot);
        }

        public bool Exist(string account, string not_activation)
        {
            if (!string.IsNullOrEmpty(not_activation))
            {
                return CONN_TopicSharing.SQLQuery<int>(string.Format(" select count(1) from Robot where account='{0}' and activation!='{1}' ", account, not_activation), null).FirstOrDefault() > 0;
            }
            else
            {
                return CONN_TopicSharing.SQLQuery<int>(string.Format(" select count(1) from Robot where account='{0}' ", account), null).FirstOrDefault() > 0;
            }
        }

        public List<Robot> GetRobots()
        {
            return CONN_TopicSharing.SQLQuery<Robot>(" select * from Robot where is_delete=0 ", null).ToList();
        }

        public List<Robot> GetRobots(string strWhere, string orderbys, int pagesize, int pageindex, out int count)
        {
            List<Robot> Robots = new List<Robot>();
            if (string.IsNullOrEmpty(strWhere))
            {
                strWhere = " is_delete=0 ";
            }
            else
            {
                strWhere += " and  is_delete=0 ";
            }
             
            if (string.IsNullOrEmpty(orderbys))
                orderbys = "r.id";

            Robots = CONN_TopicSharing.SQLPager<Robot>(" * ", " Robot ", strWhere, "", "", orderbys, pagesize, pageindex, out count, null).ToList();

            return Robots;
        }



        //        select * from (
        //select row_number()over(order by r.id)rownumber,r.*,u.username,u.nickname from Robot r 
        //left join Users u on r.user_id=u.id
        //where u.is_delete=0 and r.is_delete=0
        //)a
        //where rownumber>0 and rownumber<=2

        //注册机器人账号

        public int RegRobotAccent(Robot model)
        {

            string sql = " IF ( select count(1) from [Robot] where  account=@account)=0 ";

            sql += " update [dbo].[Robot] set account=@account,password=@password,reg_date=getdate(),last_date=GETDATE(),state=1 where [activation]=@activation ; ";
            sql += " else ";
            sql += " print -1 ";

           var res=  CONN_TopicSharing.Excute(sql, new { account = model.account, password = model.password, activation = model.activation });
            return res;
        }
        public bool Insert(List<Robot> list)
        {
            //string sql = "INSERT INTO Product(Id,Name,Price) VALUES(newid(),@p,@d)";


            Robot model = list.FirstOrDefault();
            var ps = model.GetType().GetProperties();
            List<string> @colms = new List<string>();
            List<string> @params = new List<string>();

            foreach (var p in ps)
            {
                if (!p.CustomAttributes.Any(x => x.AttributeType == typeof(XYD.Framework.IdentityAttribute)))
                {
                    @colms.Add(string.Format("[{0}]", p.Name));
                    @params.Add(string.Format("@{0}", p.Name));
                }
            }
            var sql = string.Format("INSERT INTO [{0}] ({1}) VALUES({2})", typeof(Robot).Name, string.Join(", ", @colms), string.Join(", ", @params));


            return CONN_TopicSharing.Excute(sql, list) > 0;




        }

    }
}
